﻿
import { Dialog } from 'primeng/components/dialog/dialog';
import { Component, Injector, ViewChild, OnInit } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import {  WorkStationUpdateOrOutput, PrintEquipmentServiceProxy, PrintEquipmentDto, UpdatePrintEquipment, CreatePrintEquipment, CompanyDto, OrganizationUnitServiceProxy, ComboboxItemDto } from '@shared/service-proxies/service-proxies';
import { Table } from 'primeng/components/table/table';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';


@Component({
    selector : 'app-contact',
    templateUrl: './printer.component.html',
    styleUrls: ['./printer.component.less'],
    animations: [appModuleAnimation()]
})
export class PrinterComponent extends AppComponentBase implements OnInit  {

    @ViewChild('dt') dataTable: Table;

    displayDialog: boolean;
    selectedPrinter: UpdatePrintEquipment;    
    newPrinter: boolean;    
    printers: PrintEquipmentDto[];

    companySelected: '';
    companyCombobox: ComboboxItemDto[]= [];

    constructor(
        injector: Injector,
        private _printEquipmentServiceProxy: PrintEquipmentServiceProxy,
        private _organizationUnitServiceProxy : OrganizationUnitServiceProxy
        ) 
        {
            super(injector);            
        }

    
    
    cols: any[];
    ngOnInit(): void {
        this.cols = [
            { field: 'id', header: '编号' },
            { field: 'sn', header: 'SN' },
            { field: 'displayName', header: '名称' },
            { field: 'location', header: '位置' },
            { field: 'companyDisplayName', header: '所属单位' },
            { field: 'remark', header: '备注' },
        ];

        this.getAllPrinter();
        this.initCompanyCombobox();
    }



    initCompanyCombobox()
    {
        this._organizationUnitServiceProxy.getAllCompany()
        .subscribe(result => {
            result.forEach(c => {
                let companyItem = new ComboboxItemDto();
                companyItem.value = c.code;
                companyItem.displayText = c.displayName;
                this.companyCombobox.push(companyItem);
            }); 
        });
    }
    

    getAllPrinter(){
        this._printEquipmentServiceProxy.getAll('','','','',0,200)
        .subscribe(result => {
            this.printers = result.items;
          });       

    }

    showDialogToAdd() {
        this.newPrinter = true;
        this.selectedPrinter = new UpdatePrintEquipment();
        this.selectedPrinter.sn = '000000';
        this.displayDialog = true;
    }


    save() {        

         if(this.newPrinter){            
            this._printEquipmentServiceProxy.create(this.cloneUpdateToCreate(this.selectedPrinter))
            .subscribe(result => {
                this.getAllPrinter();
            });
        }else
        {
            this._printEquipmentServiceProxy.update(this.selectedPrinter)
            .subscribe(() => {
                this.getAllPrinter();
            });

        }
       
        this.displayDialog = false;   
        
    }

    delete() {
        this._printEquipmentServiceProxy.delete(this.selectedPrinter.id)
        .subscribe(result => {
            this.getAllPrinter();
          });
        
        this.selectedPrinter = null;
        this.displayDialog = false;
    }

    onRowSelect(event) {
        this.newPrinter = false;
        this.selectedPrinter = this.cloneStation(event.data);
        this.displayDialog = true;
    }

    cloneStation(c: PrintEquipmentDto): UpdatePrintEquipment {
        let station = new UpdatePrintEquipment();
        for (let prop in c) {
            station[prop] = c[prop];
        }
        return station;
    }

    cloneUpdateToCreate(c: UpdatePrintEquipment): CreatePrintEquipment {
        let station = new CreatePrintEquipment();
        for (let prop in c) {
            if(prop!='id')
                station[prop] = c[prop];
        }
        return station;
    }
      
    print(qrcode:string,stationName:string) {
        
    }
}
